#include "budget.h"
#include "ui_budget.h"
#include<QtSql/QSqlQuery>
#include<QMessageBox>
#include<QDebug>
#include<QDesktopWidget>
//This class represents list of accounts, user will select account to assign budget

Budget::Budget(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Budget)
{
    ui->setupUi(this);
    QRect innersize=QApplication::desktop()->availableGeometry(this);
    ui->listWidget->setGeometry(0,50,(innersize.width()),(innersize.height()));
    ui->scrollArea->setGeometry(0,0,(innersize.width()),(innersize.height()));
    ui->listWidget->setEditFocus(true);
    QAction* backAction = new QAction( tr("Back"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );

     int fontSize=10;
    int count=0;
    if(db.db_connect())
    {
       //
        QSqlQuery qr;
               qr.prepare("select * from account_table");
               if(qr.exec())
               {
                   while(qr.next())
                   {
                   // budget=qr.value(3).toDouble();
                   // qDebug()<<nm<<qr.value(0).toString()<<qr.value(1).toString()<<qr.value(2).toDouble()<<qr.value(3).toDouble();
                    item1=new QListWidgetItem(qr.value(1).toString(),ui->listWidget,QListWidgetItem::UserType);
                    item1->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));
                    count++;

               }
                }
               if(count==0)
               { ui->listWidget->hide();
                   ui->label->hide();
                   QMessageBox::information(0,"Error","No account is present");

               }

        db.db_close();

         //qDebug()<<"was closing";
     }
}

Budget::~Budget()
{

    delete ui;
}
void Budget::back()
{
   (new MainWindow)->show();
}

void Budget::on_listWidget_itemActivated(QListWidgetItem* item)
{
    QString label=item->text();
    Budget_edit *edit=new Budget_edit(label);

    edit->show();

}
